/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhuoyue.elmt.entity.MaintenanceContract;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.MaintenanceContractDevice;
import com.zhuoyue.elmt.service.MaintenanceContractDeviceService;

import java.util.Date;

/**
 * 维保合同备案设备信息Controller
 *
 * @author chenhao
 * @version 2016-09-11
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/maintenanceContractDevice")
public class MaintenanceContractDeviceController extends BaseController {

    @Autowired
    private MaintenanceContractDeviceService maintenanceContractDeviceService;

    @ModelAttribute
    public MaintenanceContractDevice get(@RequestParam(required = false) String id) {
        MaintenanceContractDevice entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = maintenanceContractDeviceService.get(id);
        }
        if (entity == null) {
            entity = new MaintenanceContractDevice();
        }
        return entity;
    }

    @RequiresPermissions("elmt:maintenanceContractDevice:view")
    @RequestMapping(value = {"list", ""})
    public String list(MaintenanceContractDevice maintenanceContractDevice, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<MaintenanceContractDevice> page = maintenanceContractDeviceService.findPage(new Page<MaintenanceContractDevice>(request, response), maintenanceContractDevice);
        model.addAttribute("page", page);
        return "zhuoyue/elmt/maintenanceContractDeviceList";
    }

    @RequiresPermissions("elmt:maintenanceContractDevice:view")
    @RequestMapping(value = "form")
    public String form(MaintenanceContractDevice maintenanceContractDevice, Model model) {
        model.addAttribute("maintenanceContractDevice", maintenanceContractDevice);
        return "zhuoyue/elmt/maintenanceContractDeviceForm";
    }

    @RequiresPermissions("elmt:maintenanceContractDevice:edit")
    @RequestMapping(value = "save")
    public String save(MaintenanceContractDevice maintenanceContractDevice, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, maintenanceContractDevice)) {
            return form(maintenanceContractDevice, model);
        }
        maintenanceContractDeviceService.save(maintenanceContractDevice);
        addMessage(redirectAttributes, "保存维保合同备案设备信息成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/maintenanceContractDevice/?repage";
    }

    @RequiresPermissions("elmt:maintenanceContractDevice:edit")
    @RequestMapping(value = "delete")
    public String delete(MaintenanceContractDevice maintenanceContractDevice, RedirectAttributes redirectAttributes) {
        maintenanceContractDeviceService.delete(maintenanceContractDevice);
        addMessage(redirectAttributes, "删除维保合同备案设备信息成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/maintenanceContractDevice/?repage";
    }

    @RequestMapping(value = "findByDeviceCode")
    @ResponseBody
    public String findByDeviceCode(String deviceCode, Model model, RedirectAttributes redirectAttributes) {
        String successful = "";
        MaintenanceContract maintenanceContract = maintenanceContractDeviceService.getByDeviceCode(deviceCode);
        if (null == maintenanceContract) {
            successful = "0";
        } else if(maintenanceContract.getExpirationDate().compareTo(new Date()) < 0) {
            successful = "1";
        } else {
            successful = "2";
        }
        return successful;
    }

}